package cn.dankal.flyaudio.personal;

import android.annotation.SuppressLint;
import android.os.Bundle;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.android.arouter.launcher.ARouter;
import com.jeremyliao.liveeventbus.LiveEventBus;

import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import api.HomeServiceFactory;
import api.ShopUserServiceFactory;
import cn.dankal.basiclib.base.activity.BaseActivity;
import cn.dankal.basiclib.base.recyclerview.OnRvItemClickListener;
import cn.dankal.basiclib.pojo.BaseResponse;
import cn.dankal.basiclib.pojo.SignInEntity;
import cn.dankal.basiclib.protocol.MainProtocol;
import cn.dankal.basiclib.rx.AbstractSubscriber;
import cn.dankal.basiclib.util.ActivityUtils;
import cn.dankal.business.Constants;
import cn.dankal.business.pojo.ActivityBean;
import cn.dankal.business.pojo.HomeBean;
import cn.dankal.flyaudio.R;
import cn.dankal.flyaudio.adapter.SignRewardAdapter;
import cn.dankal.flyaudio.configs.TestData;
import cn.dankal.flyaudio.home.ActivityAdapter;

public class SignInActivity extends BaseActivity {

    private RecyclerView signRewardView;

    private SignRewardAdapter adapter;

    private RecyclerView activityView;

    private TextView tv_sign_total;
    private View activityLayoutView;

    ActivityAdapter activityAdapter = new ActivityAdapter();

    @Override
    protected int getLayoutId() {
        return R.layout.activity_sign_in;
    }

    @SuppressLint("WrongConstant")
    @Override
    protected void initComponents() {
        signRewardView = findViewById(R.id.rv_reward_list);
        adapter = new SignRewardAdapter();
        activityLayoutView = findViewById(R.id.ll_activity);
        signRewardView.setLayoutManager(new LinearLayoutManager(this, LinearLayoutManager.HORIZONTAL, false));
        signRewardView.setAdapter(adapter);
        adapter.setContinuousCount("1");
        tv_sign_total = findViewById(R.id.tv_sign_total);
        activityView = findViewById(R.id.rv_home_activity);
        activityView.setAdapter(activityAdapter);
        findViewById(R.id.iv_onback).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                finish();
            }
        });
        activityView.setLayoutManager(new LinearLayoutManager(this, LinearLayout.HORIZONTAL, false));
        adapter.addDatas(TestData.getTestData(), true);
        obtainData();
        apiGetSignIn();
        findViewById(R.id.iv_wenhao).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                Bundle bundle = new Bundle();
                bundle.putString("type","2");
                ActivityUtils.startActivity(bundle,MyCardRuleActivity.class);
            }
        });
        findViewById(R.id.tv_activity_more).setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                LiveEventBus.get("to_home_activity",String.class).post("");
                finish();
            }
        });
    }

    @Override
    public void obtainData() {
        super.obtainData();
        HomeServiceFactory.info_list("1", "3")
                .subscribe(new AbstractSubscriber<HomeBean>(SignInActivity.this) {
                    @Override
                    public void onNext(HomeBean homeBean) {
//                        super.onNext(homeBean);
                        addActivity(homeBean);
                    }
                });
    }

    private void getSignDay(){
//        ShopUserServiceFactory.doSignIn()
    }

    private void addActivity(HomeBean t) {
        if (t.vehicleActivityVOList.isEmpty()) {
            activityLayoutView.setVisibility(View.VISIBLE);
        } else {
            activityLayoutView.setVisibility(View.VISIBLE);
            activityAdapter.setOnRvItemClickListener(new OnRvItemClickListener<ActivityBean>() {
                @Override
                public void onItemClick(View v, int position, ActivityBean data) {
                    ARouter.getInstance().build(MainProtocol.ACTIVITY_DETAIL)
                            .withString(Constants.UUID, data.uuid)
                            .navigation();
                }
            });
            //活动
            activityAdapter.addDatas(t.vehicleActivityVOList,true);
        }
    }

    private  void apiGetSignIn(){
        ShopUserServiceFactory.getSignIn()
                .subscribe(new AbstractSubscriber<BaseResponse<SignInEntity>>(this) {
                    @Override
                    public void onNext(BaseResponse<SignInEntity> signInEntityBaseResponse) {
                        adapter.setContinuousCount(signInEntityBaseResponse.getData().getContinuousCount());
                        tv_sign_total.setText("已累计签到"+signInEntityBaseResponse.getData().getContinuousCount()+"天");
                        adapter.notifyDataSetChanged();
                    }
                });
    }
}
